package com.sunwayworld.basemodule.business.result.validators;

import com.sunwayworld.basemodule.business.ordertask.bean.LimsOrderTaskBean;
import com.sunwayworld.basemodule.business.ordertask.service.LimsOrderTaskService;
import com.sunwayworld.basemodule.business.result.bean.LimsResultBean;
import com.sunwayworld.basemodule.business.result.bean.LimsRunBean;
import com.sunwayworld.basemodule.business.result.service.LimsResultService;
import com.sunwayworld.framework.constant.Constant;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.StringUtils;
import com.sunwayworld.framework.validator.data.DataValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class RunSubmitAbleValidator implements DataValidator {

    @Autowired
    private LimsResultService resultService;

    @Autowired
    private LimsOrderTaskService orderTaskService;

    @Override
    public boolean doValid(Object... args) {
        RestJsonWrapperBean wrapperBean = (RestJsonWrapperBean) args[0];
        List<LimsRunBean> runBeanList = wrapperBean.parse(LimsRunBean.class);
        if (runBeanList.isEmpty()) {
            return false;
        }
        //验证结果是否录入完毕
        List<Long> runIdList = runBeanList.stream().map(LimsRunBean::getId).collect(Collectors.toList());
        //获取批次相关的orderTask
        List<LimsOrderTaskBean> orderTaskList = orderTaskService.selectListByFilter(SearchFilter.instance()
                .match("runId", runIdList).filter(MatchPattern.OR)
                .match("status", "Logged").filter(MatchPattern.SB)
                .match("flag", "1").filter(MatchPattern.EQ));
        if (orderTaskList.isEmpty()) {
            return false;
        }
        //获取相关结果
        List<Long> orderTaskIdList = orderTaskList.stream().map(LimsOrderTaskBean::getId).collect(Collectors.toList());
        List<LimsResultBean> resultList = resultService.selectListByFilter(SearchFilter.instance().match("orderTaskId", orderTaskIdList).filter(MatchPattern.OR));
        // 校验不隐藏、必填分析项是否都填写
        String regex = "^[\\s\"'“”’‘](.*?)|(.*?)[\\s\"'“”’‘]$";
        List<LimsResultBean> resultBeans = resultList.stream().filter(result -> !StringUtils.equals(result.getHideFlag(), Constant.YES) && StringUtils.equals(result.getWriteFlag(), Constant.YES)
                && (StringUtils.isBlank(result.getFinalResult()) || result.getFinalResult().matches(regex))).collect(Collectors.toList());
        if (!resultBeans.isEmpty()) {
            String msg = resultBeans.stream().map(LimsResultBean::getAnalyte).distinct().collect(Collectors.joining("<br>"));
            this.addConstraintViolation(I18nHelper.getMessage("JAVA.TIP.FOLLOWING.ANALYTE") + "<br/>" + msg + "<br/>" + I18nHelper.getMessage("JAVA.TJ.RESULT.RECORD.CHECK"));
            return false;
        }
        return true;
    }
}
